package com.jacky.morelistview.widget;


import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;


import com.jacky.morelistview.R;
import com.jacky.morelistview.bean.BaseListReturn;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by Jacky on 2017/2/20.
 */

public class MoreListView<T> extends ListView {
    private int lastVisibileItem;
    private List<T> list = new ArrayList<>();

    private boolean isLoad = false;
    private boolean isLast = false;
    public View loading;
    private boolean isHead = false;

    private OnLoadListener l;

    public interface OnLoadListener<T> {
        void onLoadMore();

        void onRefresh(List<T> datas);
    }

    public void setOnloadListener(OnLoadListener l) {
        this.l = l;
    }

    public MoreListView(Context context) {
        super(context);

    }

    public MoreListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    private void init() {
        getFooter();
        setOnScrollListener(new OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView absListView, int scrollState) {

                Log.i("@@", "dataSize" + list.size() + "load===" + isLoad + "last===" + isLast);
                if (scrollState == OnScrollListener.SCROLL_STATE_IDLE
                        && lastVisibileItem == list.size()
                        && isLoad == false && isLast == false) {
                    isLoad = true;
                    loading.setVisibility(View.VISIBLE);
                    l.onLoadMore();
                    setLast(false);
                }
            }

            @Override
            public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount,
                                 int totalItemCount) {
                lastVisibileItem = firstVisibleItem + visibleItemCount - (isHead ? 2 : 1);
            }
        });
    }

    /**
     * 添加头部
     *
     * @param view
     */
    public void addHeaderViews(View view) {
        this.isHead = true;
        this.addHeaderView(view);
    }

    /**
     * 添加底部
     *
     * @param view
     */
    public void addFooterViews(View view) {
        this.addFooterView(view);
    }

    /**
     * 是否加载
     *
     * @param load
     */
    public void setLoad(boolean load) {
        this.isLoad = load;
    }

    /**
     * 是否最后一页
     *
     * @param last
     */
    public void setLast(boolean last) {

        this.isLast = last;
    }

    /**
     * 这个要放在初始化适配器前面
     *
     * @param data
     */
    public void setData(List<T> data) {
        this.list = data;
        init();
    }

    /**
     * 网络加载完后填充数据
     *
     * @param data 网络数据源
     * @param flag 是否分页
     */
    public void setFill(T data, boolean flag) {
        BaseListReturn result = (BaseListReturn) data;
        this.setLoad(false);
        if (result.getCode() == 100) {
            if (!flag) {
                //代表分页
                list.addAll(result.getData());
            } else {
                //代表下啦，重新更换数据
                list.clear();
                list.addAll(result.getData());
            }
            if (!result.isNext()) {
                this.setLast(true);
                this.loading.setVisibility(View.GONE);
            } else {
                this.loading.setVisibility(View.VISIBLE);
            }
            if (l != null) {
                l.onRefresh(list);
            }
        } else {
            Toast.makeText(getContext(), "错误提示 自己编辑", Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 添加底部试图
     */
    private void getFooter() {
        LinearLayout footer = (LinearLayout) LayoutInflater.from(
                getContext()).inflate(R.layout.widget_footer, null);
        loading = footer.findViewById(R.id.lvbottom_loading);
        this.addFooterViews(footer);
        loading.setVisibility(View.GONE);
    }
}
